using Microsoft.Phone.Controls.Maps.AutomationPeers;
using Microsoft.Phone.Controls.Maps.Core;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Globalization;
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Data;
namespace Microsoft.Phone.Controls.Maps.Overlays
{
	[TemplatePart(Name = "LogoElement", Type = typeof(Logo)), TemplatePart(Name = "CopyrightElement", Type = typeof(Copyright)), TemplatePart(Name = "ScaleElement", Type = typeof(Scale)), TemplatePart(Name = "ZoomBarElement", Type = typeof(ZoomBar))]
	public class MapForeground : Overlay
	{
		internal const string ScaleElementName = "ScaleElement";
		internal const string CopyrightElementName = "CopyrightElement";
		internal const string LogoElementName = "LogoElement";
		internal const string ZoomBarElementName = "ZoomBarElement";
		private const int refreshMilliseconds = 500;
		private static readonly long refreshTicks = new TimeSpan(0, 0, 0, 0, 500).get_Ticks();
		private readonly MapBase targetMap;
		private Copyright copyright;
		private long lastOverlayRefresh;
		private double lastZoomLevel;
		private Logo logo;
		private MapMode previousMode;
		private Scale scale;
		private ZoomBar zoomBar;
		public static readonly DependencyProperty CultureProperty = DependencyProperty.Register("Culture", typeof(string), typeof(MapForeground), new PropertyMetadata(new PropertyChangedCallback(MapForeground.OnCultureChanged)));
		public string Culture
		{
			get
			{
				return (string)base.GetValue(MapForeground.CultureProperty);
			}
			set
			{
				base.SetValue(MapForeground.CultureProperty, value);
			}
		}
		public Copyright Copyright
		{
			get
			{
				return this.copyright;
			}
			private set
			{
				if (this.copyright != null && value != null)
				{
					value.set_Visibility(this.copyright.get_Visibility());
					value.Attributions = this.copyright.Attributions;
					value.SetForBackground = this.copyright.SetForBackground;
				}
				this.copyright = value;
			}
		}
		public Logo Logo
		{
			get
			{
				return this.logo;
			}
			private set
			{
				if (this.logo != null && value != null)
				{
					value.set_Visibility(this.logo.get_Visibility());
				}
				this.logo = value;
			}
		}
		public Scale Scale
		{
			get
			{
				return this.scale;
			}
			private set
			{
				if (this.scale != null && value != null)
				{
					value.set_Visibility(this.scale.get_Visibility());
					value.MetersPerPixel = this.scale.MetersPerPixel;
					value.DistanceUnit = this.scale.DistanceUnit;
					value.Culture = this.scale.Culture;
					value.SetForBackground = this.scale.SetForBackground;
				}
				this.scale = value;
			}
		}
		public ZoomBar ZoomBar
		{
			get
			{
				return this.zoomBar;
			}
			private set
			{
				if (this.zoomBar != null && value != null)
				{
					value.set_Visibility(this.zoomBar.get_Visibility());
				}
				if (this.zoomBar != null)
				{
					this.zoomBar.ZoomMap -= new EventHandler<MapCommandEventArgs>(this.ZoomBar_ZoomMap);
				}
				this.zoomBar = value;
				if (this.zoomBar != null)
				{
					this.zoomBar.ZoomMap += new EventHandler<MapCommandEventArgs>(this.ZoomBar_ZoomMap);
				}
			}
		}
		internal MapForeground(MapBase map)
		{
			base.set_DefaultStyleKey(typeof(MapForeground));
			this.targetMap = map;
			this.logo = new Logo();
			this.copyright = new Copyright();
			this.scale = new Scale();
			this.zoomBar = new ZoomBar();
			this.zoomBar.ZoomMap += new EventHandler<MapCommandEventArgs>(this.ZoomBar_ZoomMap);
			this.Attach();
		}
		private static void OnCultureChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			((MapForeground)d).OnCultureChanged(e.get_OldValue() as CultureInfo, e.get_NewValue() as CultureInfo);
		}
		protected virtual void OnCultureChanged(CultureInfo oldValue, CultureInfo newValue)
		{
			if (this.Scale != null && (this.Scale.Culture == null || this.Scale.Culture == oldValue))
			{
				this.Scale.Culture = newValue;
			}
		}
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			this.Logo = (base.GetTemplateChild("LogoElement") as Logo);
			this.Scale = (base.GetTemplateChild("ScaleElement") as Scale);
			this.Copyright = (base.GetTemplateChild("CopyrightElement") as Copyright);
			this.ZoomBar = (base.GetTemplateChild("ZoomBarElement") as ZoomBar);
			this.Attach();
			base.FireTemplateApplied();
		}
		internal void UpdateScale()
		{
			this.lastOverlayRefresh = DateTime.get_Now().get_Ticks();
			this.lastZoomLevel = this.targetMap.ZoomLevel;
			if (this.Scale != null)
			{
				this.Scale.MetersPerPixel = this.targetMap.Mode.Scale;
			}
		}
		internal void Attach()
		{
			if (this.Logo != null)
			{
				FrameworkElement arg_43_0 = this.Logo;
				DependencyProperty arg_43_1 = UIElement.VisibilityProperty;
				Binding binding = new Binding();
				binding.set_Mode(3);
				binding.set_Source(this.targetMap);
				binding.set_Path(new PropertyPath("LogoVisibility", new object[0]));
				arg_43_0.SetBinding(arg_43_1, binding);
			}
			if (this.Copyright != null)
			{
				FrameworkElement arg_8C_0 = this.Copyright;
				DependencyProperty arg_8C_1 = UIElement.VisibilityProperty;
				Binding binding2 = new Binding();
				binding2.set_Mode(3);
				binding2.set_Source(this.targetMap);
				binding2.set_Path(new PropertyPath("CopyrightVisibility", new object[0]));
				arg_8C_0.SetBinding(arg_8C_1, binding2);
			}
			if (this.Scale != null)
			{
				FrameworkElement arg_D5_0 = this.Scale;
				DependencyProperty arg_D5_1 = UIElement.VisibilityProperty;
				Binding binding3 = new Binding();
				binding3.set_Mode(3);
				binding3.set_Source(this.targetMap);
				binding3.set_Path(new PropertyPath("ScaleVisibility", new object[0]));
				arg_D5_0.SetBinding(arg_D5_1, binding3);
			}
			if (this.ZoomBar != null)
			{
				FrameworkElement arg_11E_0 = this.ZoomBar;
				DependencyProperty arg_11E_1 = UIElement.VisibilityProperty;
				Binding binding4 = new Binding();
				binding4.set_Mode(3);
				binding4.set_Source(this.targetMap);
				binding4.set_Path(new PropertyPath("ZoomBarVisibility", new object[0]));
				arg_11E_0.SetBinding(arg_11E_1, binding4);
			}
			this.Refresh();
			this.targetMap.ViewChangeStart += new EventHandler<MapEventArgs>(this.Map_ViewChangeStart);
			this.targetMap.ViewChangeOnFrame += new EventHandler<MapEventArgs>(this.Map_ViewChangeOnFrame);
			this.targetMap.ViewChangeEnd += new EventHandler<MapEventArgs>(this.Map_ViewChangeEnd);
			this.targetMap.ModeChanged += new EventHandler<MapEventArgs>(this.Map_ModeChange);
			this.Map_ModeChange(this.targetMap, null);
		}
		private void Attributions_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
		{
			this.UpdateAttributions();
		}
		private void UpdateAttributions()
		{
			MapForeground.SyncAttributions(this.Copyright.Attributions, this.targetMap.Mode.Attributions);
		}
		private static void SyncAttributions(IList<AttributionInfo> list, IList<AttributionInfo> desired)
		{
			if (list != null)
			{
				if (desired != null)
				{
					int i = 0;
					while (i < list.get_Count())
					{
						if (!desired.Contains(list.get_Item(i)))
						{
							list.RemoveAt(i);
						}
						else
						{
							i++;
						}
					}
					using (IEnumerator<AttributionInfo> enumerator = desired.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							AttributionInfo current = enumerator.get_Current();
							if (!list.Contains(current))
							{
								list.Add(current);
							}
						}
						return;
					}
				}
				list.Clear();
			}
		}
		private void Map_ViewChangeEnd(object sender, MapEventArgs e)
		{
			this.UpdateScale();
		}
		private void Map_ViewChangeStart(object sender, MapEventArgs e)
		{
			this.lastOverlayRefresh = DateTime.get_Now().get_Ticks();
		}
		private void Map_ViewChangeOnFrame(object sender, MapEventArgs e)
		{
			if (Math.Round(this.lastZoomLevel) != Math.Round(this.targetMap.ZoomLevel) || DateTime.get_Now().get_Ticks() - this.lastOverlayRefresh >= MapForeground.refreshTicks)
			{
				this.UpdateScale();
			}
		}
		private void Map_ModeChange(object sender, MapEventArgs e)
		{
			if (this.previousMode != null)
			{
				this.previousMode.Attributions.CollectionChanged -= new NotifyCollectionChangedEventHandler(this.Attributions_CollectionChanged);
			}
			this.previousMode = this.targetMap.Mode;
			this.previousMode.Attributions.CollectionChanged += new NotifyCollectionChangedEventHandler(this.Attributions_CollectionChanged);
			this.UpdateAttributions();
			this.Refresh();
		}
		private void Refresh()
		{
			this.UpdateScale();
			if (this.Copyright != null)
			{
				this.Copyright.SetForBackground = this.targetMap.Mode.ModeBackground;
			}
			if (this.Scale != null)
			{
				this.Scale.SetForBackground = this.targetMap.Mode.ModeBackground;
			}
		}
		private void ZoomBar_ZoomMap(object sender, MapCommandEventArgs e)
		{
			if (e.Command != null)
			{
				e.Command.Execute(this.targetMap);
			}
		}
		protected override AutomationPeer OnCreateAutomationPeer()
		{
			return new BaseAutomationPeer(this, "MapForeground");
		}
	}
}
